const { query } = require('../../config/mysqlPool')

const qTable = 'select * from loan', orderBy = 'order by create_time', limit = 'desc limit ?,?'
exports.findLoanListByName = (name, limitA, limitB) => query(`${qTable} where name like ? ${orderBy} ${limit} `, [`%${name}%`, limitA, limitB]);
exports.findLoanList = (limitA, limitB) => query(`${qTable} ${orderBy} ${limit}`, [limitA, limitB]);
exports.getLoanCountByName = name => query('select count(*) as total from loan where name like ?', [`%${name}%`])
exports.getLoanCount = () => query('select count(*) as total from loan')
exports.insertLoan = o => {
  const keys = Object.keys(o);
  const values = Object.values(o)
  return query(`insert into loan (${keys.join(',')}) values (${new Array(values.length).fill('?').join(',')})`, values)
}

exports.updateLoanSqlFun = (o, id) => {
  const keys = Object.keys(o);
  const values = Object.values(o)
  return query(`update loan set update_time=now(),${keys.join('=?,') + '=?'} where id = ?`, [...values, id])
}
exports.deleteLoanSqlFun = id => query(`delete from loan  where id = ?`, [id])